package SqlDemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

public class user {


    //连接池
    private static LinkedList<Connection> linkedList = new LinkedList<Connection>();


    private static final String url = "jdbc:mysql://127.0.0.1:3306/linyujie?useUnicode=true&amp;characterEncoding=utf-8";
    private static final String username = "root";
    private static final String password = "123456";
    //静态代码块
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getInstace() throws SQLException {

        if (linkedList.isEmpty()){
            for (int i = 0; i < 10 ; i++) {
                Connection con = DriverManager.getConnection(url,username,password);
                linkedList.add(con);
            }
        }
        return linkedList.poll();

    }

    //归还连接对象
    public static void  huancon(Connection con){
        linkedList.push(con);
    }
}
